#!/usr/bin/perl
#copyright Ian Baker 2007

use POSIX;
use Time::HiRes qw (usleep);

#open(SER, '+</dev/ttyS0') || die "Couldn't open /dev/ttyS0: $!";
sysopen(SER, "/dev/ttyS0", O_RDWR|O_NONBLOCK|O_NOCTTY|O_EXCL) ||
  die "Couldn't open /dev/ttyS0: $!";
#open(SERO, "+>&SER")
#    or die "can't dup SER: $!";
my $ofh = select(SER); $| = 1; select($ofh);
$| = 1;

print "1\n";

my $t = POSIX::Termios->new;
$t->getattr(fileno(SER));
$t->setispeed('B9600');
$t->setospeed('B9600');
$t->setattr(fileno(SER), TCSANOW);

while(1) {
  foreach(0..128) {
    #print SER chr($_);
    syswrite(SER, chr($_), 1) || warn "$!";
    my $out;
    read(SER, $out, 1);
    print "$_:" . ord($out) . "\n";
    usleep(100);
  }
}  
